﻿#include "bizadd.h"
#include "ui_bizadd.h"

BizAdd::BizAdd(QWidget *parent) :
    ItemAdd(parent),
    ui(new Ui::BizAdd)
{
    ui->setupUi(this);

    newFlag=false;
    table=TD_REJECT_INDEX;
    detailTable=TD_REJECT_DETAIL;

    _uid=QUuid::createUuid().toString();
    model=new QSqlTableModel(this);
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    ui->tableView->setModel(model);
}

BizAdd::~BizAdd()
{
    delete ui;
}

void BizAdd::updateView(const QString &uid)
{
    model->setTable(detailTable);
    model->setFilter(QString("uid='%1'").arg(uid));
    model->setSort(0,Qt::DescendingOrder);
    model->select();

    ui->tableView->hideColumn(0);
    ui->tableView->hideColumn(1);
    model->setHeaderData(2,Qt::Horizontal,QStringLiteral("名称"));
    model->setHeaderData(3,Qt::Horizontal,QStringLiteral("数量"));
    ui->tableView->hideColumn(4);
    model->setHeaderData(5,Qt::Horizontal,QStringLiteral("检查状况"));
}

bool BizAdd::saveIndex()
{
    QSqlQuery query;
    query.prepare("insert into td_reject_index (uid,date,contact,phone,express,status,remark) values (?,?,?,?,?,?,?)");
    query.addBindValue(_uid);
    query.addBindValue(Env::today);
    query.addBindValue(contact);
    query.addBindValue(phone);
    query.addBindValue(express);
    query.addBindValue(ST_WAIT);
    query.addBindValue(remark);

    return query.exec();
}

bool BizAdd::saveDetail()
{
    QSqlQuery query;
    query.prepare("insert into td_reject_detail (uid,name,number,status,situation) values (?,?,?,?,?)");
    query.addBindValue(_uid);
    query.addBindValue(name);
    query.addBindValue(number);
    query.addBindValue(ST_WAIT);
    query.addBindValue(situation);

    return query.exec();
}

void BizAdd::closeEvent(QCloseEvent *)
{
    if(newFlag)
        sendMsg(T_REJECT ,REJECT_NOTICE);
}

void BizAdd::on_btn_save_clicked()
{
    model->submitAll();
}

void BizAdd::on_btn_submit_clicked()
{
    if(ui->contactPan->isEnabled())
    {
        contact=ui->contact->text().trimmed();
        express=ui->express->text().trimmed();
        if(contact.isEmpty() || express.isEmpty())
        {
            QMessageBox::warning(this,QStringLiteral("不能为空"),QStringLiteral("联系方式、物流方式不能为空！"));
            return;
        }
        phone=ui->phone->text().trimmed();

        if(!saveIndex())
        {
            QMessageBox::warning(this,QStringLiteral("数据库异常"),QStringLiteral("无法保存客户信息！"));
            return;
        }
        pair.first=PAIR_INDEX;

        name=ui->name->text().trimmed();
        number=ui->number->text().trimmed();
        if(name.isEmpty() || number.isEmpty())
        {
            QMessageBox::warning(this,QStringLiteral("不能为空"),QStringLiteral("名称、数量不能为空！"));
            return;
        }
        situation=ui->situation->text().trimmed();

        if(saveDetail())
        {
            newFlag=true;
            pair.second=PAIR_DETAIL;

            updateView(_uid);
            ui->name->clear();
            ui->number->clear();
            ui->situation->clear();

            if(pair.first == PAIR_INDEX && pair.second== PAIR_DETAIL)
                ui->contactPan->setEnabled(false);
        }
        else
            QMessageBox::critical(this,QStringLiteral("异常情况"),QStringLiteral("异常情况，提交失败！"));
    }
    else
    {
        name=ui->name->text().trimmed();
        number=ui->number->text().trimmed();
        if(name.isEmpty() || number.isEmpty())
        {
            QMessageBox::warning(this,QStringLiteral("不能为空"),QStringLiteral("名称、数量不能为空！"));
            return;
        }
        situation=ui->situation->text().trimmed();

        if(saveDetail())
        {
            updateView(_uid);
            ui->name->clear();
            ui->number->clear();
            ui->situation->clear();
        }
        else
            QMessageBox::critical(this,QStringLiteral("异常情况"),QStringLiteral("异常情况，提交失败！"));
    }
}
